//
//  PlayerInfoViewController.m
//  KG
//
//  Created by Eugene Braginets on 30.03.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PlayerInfoViewController.h"
#import "AppDelegate.h"
#import "PlayerItem.h"
#import "bannerView.h"

@implementation PlayerInfoViewController
@synthesize bannerViewController=_bannerViewController;
@synthesize playerImage=_playerImage;
@synthesize activityIndicator = _activityIndicator;
@synthesize careerWebView = _careerWebView;
@synthesize playerItem=_playerItem;
@synthesize image=_image;
@synthesize itemid=_itemid;
@synthesize playerInfoView = _playerInfoView;

-(id)initWith:(PlayerItem*) dataItem {
    
    self = [super initWithNibName:@"PlayerInfoViewController" bundle:nil];
    if (self) {
        self.playerItem=dataItem;
        self.image=dataItem.image;
        self.itemid=dataItem.itemid;
        
        
        
           }
    return self;
}


- (void) updateUI {
    NSString* age=@"";
    NSString* club=@"";
    NSString* nick=@"";
    NSString* height=@"";
    NSString* weight=@"";
    NSString* role=@"";
    NSString* occupation=@"";
    NSString* career=@"";
    
    
    
    if ([self.playerItem.career length]>0) {
        career=[NSString stringWithFormat:@"<i><b>Career:</b></i><br/> %@",self.playerItem.career];
    }
    
    if ([self.playerItem.age length]>0) {
        age=[NSString stringWithFormat:@"<i>Age:</i> %@<br/>",self.playerItem.age];
    }
    
   
    if ([self.playerItem.nickname length]>0) {
        nick=[NSString stringWithFormat:@"<h3>\"%@\"</h3>",self.playerItem.nickname];
    }
    if ([self.playerItem.height length]>0) {
        height=[NSString stringWithFormat:@"<i>Height:</i> %@<br/>",self.playerItem.height];
    }
    if ([self.playerItem.weight length]>0) {
        weight=[NSString stringWithFormat:@"<i>Weight:</i> %@<br/>",self.playerItem.weight];
    }
    if ([self.playerItem.club length]>0) {
        weight=[NSString stringWithFormat:@"<b><i>Club:</i> %@</b><br/>",self.playerItem.club];
    }
    
    if ([self.playerItem.role length]>0) {
        role=[NSString stringWithFormat:@"<i>Role:</i> %@<br/>",self.playerItem.role];
    }
    
    NSString* info=[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@",club,nick,age,role,height,weight,occupation];
    
//    NSLog(@"info: %@",info);
    
    [self.careerWebView loadHTMLString:self.playerItem.career baseURL:nil];
    
    NSString* str=[NSString stringWithFormat:@"<font face=\"arial\"/><div style=\"float: right; clear: left; padding:6px;\"><IMG alt=\"loading...\" src=\"%@\" width=\"150px\"></div> %@ <br/><br/><br/>%@",self.playerItem.imageAddr,info,career];
    [self.careerWebView loadHTMLString:str baseURL:nil];
    
}


- (void) reload:(NSNotification*) notify {
    NSLog (@"reload %@%@",kPlayerItem,self.itemid);
    
    self.playerItem= [[[myAppDelegate dataArraysStore] objectForKey:[NSString stringWithFormat:@"%@%@",kPlayerItem,self.itemid]] objectAtIndex:0];
    
    [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];

  
         
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
     
    //[self.careerWebView setAlpha:0];
    [self.careerWebView setHidden:YES];
    [self.careerWebView setBackgroundColor:[UIColor clearColor]];
    [self.careerWebView setOpaque:NO];
    [self.activityIndicator setHidden:NO];
    [self.activityIndicator startAnimating];
    
    NSString* name=[NSString stringWithFormat:@"%@%@",kPlayerItem,self.playerItem.itemid];
    
//    NSLog(@"name: %@",[self.playerItem name]);
    
    if ([[myAppDelegate dataArraysStore] objectForKey:name]!=nil) {
    
        [self reload:nil];
    }
    else {
    [[NSNotificationCenter defaultCenter] addObserver:self 
         selector: @selector(reload:)
             name:name 
           object: nil];
    }
    
    name=nil;
    
    bannerView *bannerVC=[[bannerView alloc] init];
    [self.view addSubview:bannerVC.view];
    [bannerVC animate];
    self.bannerViewController=bannerVC;
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload {
    [self setPlayerImage:nil];
    [self setCareerWebView:nil];
    [self setActivityIndicator:nil];
    [self setPlayerInfoView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [[NSNotificationCenter defaultCenter] 
     removeObserver:self
     name:[NSString stringWithFormat:@"%@%@",kPlayerItem,self.playerItem.itemid]
     object:nil];
    [self.careerWebView stopLoading];
    self.careerWebView.delegate = nil;
    
    [super viewWillDisappear:animated];
}


#pragma mark - WebView Delegate Methods

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
  //  self.careerWebView.frame = CGRectMake(320, 10, 315, 350);
    self.careerWebView.hidden = NO;
    self.careerWebView.alpha=0;
    [UIView animateWithDuration:0.4 animations:^{
//        self.careerWebView.frame = CGRectMake(0, 10, 315, 350);
        self.careerWebView.alpha=1;
    }];
    [self.activityIndicator setHidden:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)likePlayerButton:(id)sender {
}
@end
